package com.system.seckillgoods.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.system.common.bean.SeckillResult;
import com.system.common.exception.ServiceException;
import com.system.common.service.ISeckillResultService;
import com.system.seckillgoods.mapper.SeckillResultMapper;
import org.apache.dubbo.config.annotation.DubboService;

import java.util.List;

@DubboService
public class SeckillResultServiceImpl extends ServiceImpl<SeckillResultMapper, SeckillResult>
        implements ISeckillResultService {

    @Override
    public SeckillResult getSeckillResultById(Integer id) {
        return super.getById(id);
    }

    @Override
    public SeckillResult getSeckillResultByUserAndSeckill(Integer userId, Integer seckillId, Integer activityId) {
        LambdaQueryWrapper<SeckillResult> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SeckillResult::getUserid, userId)
                .eq(SeckillResult::getSeckillid, seckillId)
                .eq(SeckillResult::getActivityid, activityId);
        return super.getOne(queryWrapper);
    }

    @Override
    public void addSeckillResult(SeckillResult seckillResult) {
        // 校验用户是否已经秒杀过同一活动同一商品
        if (hasUserSeckilled(seckillResult.getUserid(), seckillResult.getSeckillid(), seckillResult.getActivityid())) {
            throw new ServiceException("用户已秒杀过该商品，不能重复秒杀");
        }
        super.save(seckillResult);
    }

    @Override
    public void updateSeckillResult(SeckillResult seckillResult) {
        SeckillResult exist = super.getById(seckillResult.getId());
        if (exist == null) {
            throw new ServiceException("秒杀结果不存在");
        }
        super.updateById(seckillResult);
    }

    @Override
    public void deleteSeckillResult(Integer id) {
        SeckillResult result = super.getById(id);
        if (result == null) {
            throw new ServiceException("秒杀结果不存在");
        }
        result.setIsdel(1);
        super.updateById(result);
    }

    @Override
    public List<SeckillResult> getSeckillResultList() {
        LambdaQueryWrapper<SeckillResult> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SeckillResult::getIsdel, 0);
        return super.list(wrapper);
    }

    @Override
    public IPage<SeckillResult> getPage(SeckillResult seckillResult, Page<SeckillResult> page) {
        LambdaQueryWrapper<SeckillResult> queryWrapper = new LambdaQueryWrapper<>();
        if (seckillResult.getUserid() != null) {
            queryWrapper.eq(SeckillResult::getUserid, seckillResult.getUserid());
        }
        if (seckillResult.getGoodsid() != null) {
            queryWrapper.eq(SeckillResult::getGoodsid, seckillResult.getGoodsid());
        }
        if (seckillResult.getActivityid() != null) {
            queryWrapper.eq(SeckillResult::getActivityid, seckillResult.getActivityid());
        }
        queryWrapper.eq(SeckillResult::getIsdel, 0); // 只查询未删除的
        return super.page(page, queryWrapper);
    }

    @Override
    public boolean hasUserSeckilled(Integer userId, Integer seckillId, Integer activityId) {
        return getSeckillResultByUserAndSeckill(userId, seckillId, activityId) != null;
    }
}
